package se.id.vrapp;

import android.app.Activity;
import android.content.ClipData;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class AudioMenuOrderActivity extends Activity {
private TextView audioMenuElement1, audioMenuElement2, audioMenuElement3, audioMenuField1, audioMenuField2, audioMenuField3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.audio_order_activity);
		
		audioMenuElement1 = (TextView)findViewById(R.id.audio_menu_element_1);
		audioMenuElement2 = (TextView)findViewById(R.id.audio_menu_element_2);
		audioMenuElement3 = (TextView)findViewById(R.id.audio_menu_element_3);
		
		audioMenuElement1.setOnTouchListener(new ChoiceTouchListener());
		audioMenuElement2.setOnTouchListener(new ChoiceTouchListener());
		audioMenuElement3.setOnTouchListener(new ChoiceTouchListener());
		
		audioMenuElement1.setOnDragListener(new ChoiceDragListener());
		audioMenuElement2.setOnDragListener(new ChoiceDragListener());
		audioMenuElement3.setOnDragListener(new ChoiceDragListener());
		
		/* Original code (where there were 3 drop fields and 3 menu elements that could be dragged.)
		
		//Views (boxes) to move around
		audioMenuElement1 = (TextView)findViewById(R.id.audio_menu_element_1);
		audioMenuElement2 = (TextView)findViewById(R.id.audio_menu_element_2);
		audioMenuElement3 = (TextView)findViewById(R.id.audio_menu_element_3);
		
		//Views (fields) to move boxes between
		audioMenuField1 = (TextView)findViewById(R.id.audio_menu_field_1);
		audioMenuField2 = (TextView)findViewById(R.id.audio_menu_field_2);
		audioMenuField3 = (TextView)findViewById(R.id.audio_menu_field_3);
	
		audioMenuElement1.setOnTouchListener(new ChoiceTouchListener());
		audioMenuElement2.setOnTouchListener(new ChoiceTouchListener());
		audioMenuElement3.setOnTouchListener(new ChoiceTouchListener());
		
		audioMenuField1.setOnDragListener(new ChoiceDragListener());
		audioMenuField2.setOnDragListener(new ChoiceDragListener());
		audioMenuField3.setOnDragListener(new ChoiceDragListener());
		
		 */
		/*End of Original Code*/
	}

	private final class ChoiceTouchListener implements OnTouchListener {
		
		@Override
		public boolean onTouch(View view, MotionEvent motionEvent) {
			// TODO Auto-generated method stub
			if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
				ClipData data = ClipData.newPlainText("", "");
				DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
				
				view.startDrag(data, shadowBuilder, view, 0);
				return true;
			}
			else {
			    return false;
			}
		}
	
	}	
	
	private class ChoiceDragListener implements OnDragListener {
		
		@Override
		public boolean onDrag(View v, DragEvent event) {
			switch (event.getAction()) {
		    case DragEvent.ACTION_DRAG_STARTED:
		        //no action necessary
		        break;
		    case DragEvent.ACTION_DRAG_ENTERED:
		        //no action necessary
		        break;
		    case DragEvent.ACTION_DRAG_EXITED:
		        //no action necessary
		        break;
		    case DragEvent.ACTION_DROP:
		    	
		    	//View being dropped upon
		    	View view = (View) event.getLocalState();
		    	
		    	//Dropping on source view - do nothing
		    	if(view == v)
		    		break;
	
		    	//TextView being dropped upon
		    	TextView dropTarget = (TextView) v;
		    	//TextView being dragged
		    	TextView dropped = (TextView) view;		    	
		    	//Text of dragged view
		    	CharSequence text = dropped.getText();
		    	
		    	dropped.setText(dropTarget.getText());
		    	dropTarget.setText(text);		    	
		    	
		    	//Does the 
		    	
		    	break;
		    	
		    case DragEvent.ACTION_DRAG_ENDED:
		        //no action necessary
		        break;
		    default:
		        break;
		}
			return true;
		}
		
	}
}
